package com.llkj.qingnianguoxue.mine;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.llkj.qingnianguoxue.R;
import com.llkj.qingnianguoxue.adapter.SystemMessageAdapter;
import com.llkj.qingnianguoxue.base.BaseActivity;
import com.llkj.qingnianguoxue.base.ConstantUserInfo;
import com.llkj.qingnianguoxue.base.MyApplication;
import com.llkj.qingnianguoxue.bean.SystemMessageListBean;
import com.llkj.qingnianguoxue.http.HttpMethod;
import com.llkj.qingnianguoxue.util.ClickViewCallback;
import com.llkj.qingnianguoxue.util.DialogUtil;
import com.llkj.qingnianguoxue.util.GsonUtil;
import com.llkj.qingnianguoxue.util.helper.LoadDataHelper;

import java.util.ArrayList;
import java.util.List;

/**
 * 系统消息
 */
public class SystemMessageActivity extends BaseActivity implements BaseQuickAdapter.RequestLoadMoreListener,
        LoadDataHelper.LoadMoreListener, SwipeRefreshLayout.OnRefreshListener{
    private SwipeRefreshLayout refreshLayout;
    private RecyclerView lvMessage;
    private SystemMessageAdapter messageAdapter;
    private List<SystemMessageListBean.SystemMessageBean> messageData;
    private int totalSize, mPage, mCurrentPosition;
    private int mReceiveRefuse;//2接受，3拒绝

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_system_message);
        setTitle("消息中心", true, TITLE_TYPE_IMG, R.drawable.icon_back, false, TITLE_TYPE_TEXT, null);

        initView();
        initListener();

        loadWaitDialog();
        onRefresh();
        setMessageState();
    }

    private void initView() {
        initStatusbar(TSATUSBAR_TYPE_BLACK_TEXT,rlTitle,R.color.gray_bar);
        refreshLayout = (SwipeRefreshLayout) findViewById(R.id.refresh_layout);
        refreshLayout.setColorSchemeResources(R.color.blue3989ff);
        lvMessage = (RecyclerView) findViewById(R.id.recyclerView);
        lvMessage.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));

        messageData = new ArrayList<>();
        messageAdapter = new SystemMessageAdapter(R.layout.item_system_message, messageData, null);
        lvMessage.setAdapter(messageAdapter);
    }

    private void initListener() {
        refreshLayout.setOnRefreshListener(this);
        messageAdapter.setOnLoadMoreListener(this);
        lvMessage.addOnItemTouchListener(new OnItemChildClickListener() {
            @Override
            public void onSimpleItemChildClick(BaseQuickAdapter baseQuickAdapter, View view, int position) {
                mCurrentPosition = position;
                SystemMessageListBean.SystemMessageBean messsage = messageData.get(position);
                messsage.setIs_read("1");
                if(view.getId() == R.id.tvDelItem){
                    mCurrentPosition = position;
                    dialogConfirm("您要删除该消息吗？", null, "取消", "删除").show();
                }else if(view.getId() == R.id.tv_accept){
                    mReceiveRefuse = 2;
                    setAcceptRefuse(messsage.getForm_id(), "1",messsage.getType(),messsage.getContent_id());
                }else if(view.getId() == R.id.tv_refuse && !"3".equals(messsage.getState())){
                    mReceiveRefuse = 3;
                    setAcceptRefuse(messsage.getForm_id(), "2",messsage.getType(),messsage.getContent_id());
                }
            }
        });
    }

//    private void joinCircle(){
//        DialogUtil.dialogConfirm(this, "加入圈子需支付50元\n费用时限30天", null, "考虑一下", "支付", new DialogUtil.DialogClickListener() {
//            @Override
//            public void dialogClick(int type) {
//                if(type==1){
////                    payMethodViewUtil.showPayMethodDialog("0.1");
//                }
//            }
//        }).show();
//    }

    @Override
    protected void dialogRightBtn() {
        super.dialogRightBtn();
        SystemMessageListBean.SystemMessageBean messsage = messageData.get(mCurrentPosition);
        loadWaitDialog();
        HttpMethod.delMessage(this, this, messsage.getId());
    }

    @Override
    public void onLoadMore() {
        mPage++;
        getMessageData();
    }

    @Override
    public void onRefresh() {
        mPage = 1;
        getMessageData();
    }

    @Override
    public void onLoadMoreRequested() {
        LoadDataHelper.loadMoreRequest(this, messageAdapter, null, lvMessage, totalSize);
    }

    @Override
    protected void onhttpSuccess(String data, int method) {
        super.onhttpSuccess(data, method);
        if (method == HttpMethod.HTTP_USER_MESSAGE) {//系統消息
            SystemMessageListBean messageListBean = GsonUtil.GsonToListBean(data, SystemMessageListBean.class);
            if (messageListBean != null) {
                totalSize = Integer.parseInt(messageListBean.getNum());
                if (mPage == 1) {
                    messageData.clear();
                    LoadDataHelper.refreshComplete(messageAdapter, messageData, refreshLayout, lvMessage);
                } else {
                    messageAdapter.setEnableLoadMore(true);
                    messageAdapter.loadMoreComplete();
                }
                messageData.addAll(messageListBean.getList());
                messageAdapter.notifyDataSetChanged();
            }
        } else if (method == HttpMethod.HTTP_USER_ADD_FRIEND) {//8.4接受和不接受好友申请
            SystemMessageListBean.SystemMessageBean messsage = messageData.get(mCurrentPosition);
            messsage.setState(String.valueOf(mReceiveRefuse));
            messageAdapter.notifyItemChanged(mCurrentPosition);
            setSystemUnread();
        } else if (method == HttpMethod.HTTP_USER_DEL_MESSAGE) {//刪除消息
            SystemMessageListBean.SystemMessageBean messsage = messageData.get(mCurrentPosition);
            if (!"1".equals(messsage.getIs_read())) {
                setSystemUnread();
            }
            messageData.remove(mCurrentPosition);
            messageAdapter.notifyDataSetChanged();
        } else if (method == HttpMethod.HTTP_USER_MESSAGE_STATE) {//
            setSystemUnread();
        }
    }

    @Override
    protected void onhttpFailed(String message, int method) {
        if(method != HttpMethod.HTTP_USER_MESSAGE_STATE){
            super.onhttpFailed(message, method);
        }
    }

    private void setSystemUnread() {
        int num = 0;
        MyApplication.getInstance().sputil.put(ConstantUserInfo.IS_NEW_SYS_MSG, num);
    }

    /**
     * 获取消息数据
     */
    private void getMessageData() {
        HttpMethod.userMessage(this, this, String.valueOf(mPage));
    }

    /**
     * 接受拒绝
     *
     * @param formId 好友id
     * @param type   1接受 2 不接受
     * @param messageType   1申請好友 9 邀请加入圈子
     */
    private void setAcceptRefuse(String formId, String type,String messageType,String contentId) {
        if("1".equals(messageType)){
            loadWaitDialog();
            HttpMethod.userAddFriend(this, this, formId, type);
        }else if("9".equals(messageType)){
            loadWaitDialog();
            HttpMethod.isCircle(this,this,formId,messageType,contentId,type);
        }
    }

    private void setMessageState() {
        HttpMethod.messageState(this, this);
    }
}
